package com.example.cslp.util;

import com.example.cslp.exception.handler.ServiceException;
import com.example.cslp.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;

@Slf4j
public class VideoUtil {

    public static String getVideoPicture(String videoPath, String imgPath) throws Exception{
        log.debug("videoPath:{}",videoPath);
        File imgFile = new File(imgPath);
        //判断保存的文件的文件夹是否存在，不存在创建。
        if (!imgFile.getParentFile().exists()) {
            log.info("保存文件的文件夹不存在，创建。");
            imgFile.getParentFile().mkdirs();
        }
        String str=videoPath.replaceAll("\\\\","/");
        log.debug("str:{}",str);
        File videoFile = new File(str);
        if (videoFile.exists()) {
            log.info("视频存在：{}", videoFile);
            //实例化“截取视频首帧”对象
            FFmpegFrameGrabber ff = new FFmpegFrameGrabber(videoFile);
            ff.start();
            int width = ff.getImageWidth();
            int height = ff.getImageHeight();
            System.out.println("宽：" + width + "；高：" + height);

            long ftp = ff.getLengthInFrames();
            System.out.println("一共有：" + ftp + "帧");
            // 视频帧率
            double videoFrameRate = ff.getVideoFrameRate();
            System.out.println("帧率：" + videoFrameRate);
            long delayedTime = ff.getLengthInTime() / 1000000;
            System.out.println("秒数：" + delayedTime);
            int step = 5;
            System.out.println("每隔：" + step + "帧取一帧");
            long count = ftp / step;

//                long time = (step * (i + 1)) * 1000000L;
//                ff.setTimestamp(time);
                ff.setFrameNumber((int) (step  + 1));
                Frame f = ff.grabImage();
                //生成文件名称
                String fileName = imgPath +  UuidUtil.getUuid().toString() + ".jpg";
                log.debug("fileName:{}",fileName);
                System.out.println("截图保存：" +fileName);
                File out = new File(fileName);
                ImageIO.write(frameToBufferedImage(f), "jpg", out);

            ff.close();
            ff.stop();
            return fileName;
        } else {
            log.info("视频不存在：{}", videoFile);
        }
        throw new ServiceException(ServiceCode.ERR_UNKNOWN,"待截图的视频不存在");
    }

    /**
     * 帧转为流
     *
     * @param frame
     * @return
     */
    private static RenderedImage frameToBufferedImage(Frame frame) {
        //创建BufferedImage对象
        Java2DFrameConverter converter = new Java2DFrameConverter();
        BufferedImage bufferedImage = converter.getBufferedImage(frame);
        return bufferedImage;
    }

//    public static void main(String[] args) {
//        try {
//            String videoPath = "E:\\ffmpegTest\\smgnews.mp4";
//            String imgPath = "E:\\ffmpegTest\\img\\";
//            getVideoPicture(videoPath, imgPath);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

}
